package com.may.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.experimental.UtilityClass;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @program: spring-intepay-cashier
 * @description:
 * @author: Bobby.Ma
 * @create: 2020-07-29 18:15
 **/
@UtilityClass
public class HttpUtil {

    /**
        * @Description: post 请求
        * @param url
        * @param postData
        * @return: cn.hutool.http.HttpResponse
        * @Author: Bobby.Ma
        * @Date: 2020/8/3 18:53
    */
    public HttpResponse post(String url, Map<String, Object> postData) {
        return HttpRequest.post(url)
                .timeout(30000)
                .form(postData)
                //不写，默认就是 application/x-www-form-urlencoded
                .contentType("application/x-www-form-urlencoded").execute();//
    }

    public HttpResponse post(String url, String jsonData) {
        return HttpRequest.post(url)
                .timeout(30000)
                .body(jsonData)
                .contentType("application/json").execute();
    }

    /**
     * @param request
     * @Description: 获取post请求体中的参数
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Bobby.Ma
     * @Date: 2020/8/3 14:05
     */
    public JSONObject fetchPostData(HttpServletRequest request) {
        JSONObject res = new JSONObject();
        if ("application/x-www-form-urlencoded".equals(request.getContentType())) {
            Map<String, String[]> parameterMap = request.getParameterMap();
            if (parameterMap != null) {
                for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                    res.put(entry.getKey(), entry.getValue()[0]);
                }
            }
        } else if ("application/json".equals(request.getContentType())) {
            try {
                String paramJson = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
                JSONObject jsonObject = JSON.parseObject(paramJson);
                res.putAll(jsonObject);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    /**
     * @param request
     * @Description: 获取 get 请求参数
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Bobby.Ma
     * @Date: 2020/8/3 14:08
     */
    public JSONObject fetchGetData(HttpServletRequest request) {
        JSONObject res = new JSONObject();
        Map<String, String[]> parameterMap = request.getParameterMap();
        if (parameterMap != null) {
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                res.put(entry.getKey(), entry.getValue()[0]);
            }
        }
        return res;
    }

    public JSONObject fetchRequestData(HttpServletRequest request){
        if ("GET".equals(request.getMethod())){
            return fetchGetData(request);
        }else if ("POST".equals(request.getMethod())){
            return fetchPostData(request);
        }
        return null;
    }
}
